<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../">
  <title data-ice="title">src/PlatformManager.js | live2d-widget.js</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<meta name="description" content="Add the Sseexxyyy live2d to webpages."><meta property="twitter:card" content="summary"><meta property="twitter:title" content="live2d-widget.js"><meta property="twitter:description" content="Add the Sseexxyyy live2d to webpages."></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/xiazeyu/live2d-widget.js"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/index.js~L2Dwidget.html">L2Dwidget</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-PlatformManager">PlatformManager</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-captureFrame">captureFrame</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-theRealInit">theRealInit</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-cManager">cManager</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-cModel">cModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createElement">createElement</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getCurrentPath">getCurrentPath</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-cDefine">cDefine</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-currWebGL">currWebGL</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#config">config</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-configApplyer">configApplyer</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#lib">lib</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-EYE_STATE">EYE_STATE</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DBaseModel">L2DBaseModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DExpressionMotion">L2DExpressionMotion</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DExpressionParam">L2DExpressionParam</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DEyeBlink">L2DEyeBlink</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DMatrix44">L2DMatrix44</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DModelMatrix">L2DModelMatrix</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DMotionManager">L2DMotionManager</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DPartsParam">L2DPartsParam</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DPhysics">L2DPhysics</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DPose">L2DPose</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DTargetPoint">L2DTargetPoint</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-L2DViewMatrix">L2DViewMatrix</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-Live2DFramework">Live2DFramework</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils">utils</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-MatrixStack">MatrixStack</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-ModelSettingJson">ModelSettingJson</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">src/PlatformManager.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">
/**
 *
 *  You can modify and use this source freely
 *  only for the development of application related Live2D.
 *
 *  (c) Live2D Inc. All rights reserved.
 */

// Modified by xiazeyu.

/**
* @desc A library that provide basic IO and json function
*/

import { currWebGL } from &apos;./elementMgr&apos;;
import { Live2DModelWebGL } from &quot;./lib/live2d.core&quot;;


//============================================================
//============================================================
//  class PlatformManager     extend IPlatformManager
//============================================================
//============================================================

/**
* @name PlatformManager
* @desc Define the variable type of PlatformManager
* @param null
* @returns {Structure} PlatformManager
*/
export function PlatformManager()
{

}


//============================================================
//    PlatformManager # loadBytes()
//============================================================

/**
* @name loadBytes
* @desc load bytes from the path and callback
* @param {String} path, {Function} callback
* @returns callback {raw} context
* @memberOf PlatformManager
*/

PlatformManager.prototype.loadBytes       = function(path/*String*/, callback)
{
    var request = new XMLHttpRequest();
    request.open(&quot;GET&quot;, path, true);
    request.responseType = &quot;arraybuffer&quot;;
    request.onload = function(){
        switch(request.status){
        case 200:
            callback(request.response);
            break;
        default:
            console.error(&quot;Failed to load (&quot; + request.status + &quot;) : &quot; + path);
            break;
        }
    }
    request.send(null);
    // return request;
}


//============================================================
//    PlatformManager # loadString()
//============================================================

/**
* @name loadString
* @desc load bytes from the path and put it into buffer
* @param {String} path
* @returns buffer {raw} context
* @memberOf PlatformManager
*/
PlatformManager.prototype.loadString      = function(path/*String*/)
{

    this.loadBytes(path, function(buf) {
        return buf;
    });

}


//============================================================
//    PlatformManager # loadLive2DModel()
//============================================================

/**
* @name loadLive2DModel
* @desc load Live2DModel from the path and put it into buffer
* @param {String} path, {function} callback
* @returns callback loaded model
* @memberOf PlatformManager
*/
PlatformManager.prototype.loadLive2DModel = function(path/*String*/, callback)
{
    var model = null;

    // load moc
    this.loadBytes(path, function(buf){
        model = Live2DModelWebGL.loadModel(buf);
        callback(model);
    });

}


//============================================================
//    PlatformManager # loadTexture()
//============================================================

/**
* @name loadTexture
* @desc load Live2DModel&apos;s Texture and callback
* @param {Live2DModelWebGL}model, {int}no, {string}path, {function}callback
* @returns callback
* @memberOf PlatformManager
*/
PlatformManager.prototype.loadTexture     = function(model/*ALive2DModel*/, no/*int*/, path/*String*/, callback)
{
    // load textures
    var loadedImage = new Image();
    // Thanks to @mashirozx &amp; @fghrsh
    // Issues:
    // @https://github.com/journey-ad/live2d_src/issues/1
    // @https://github.com/journey-ad/live2d_src/issues/3
    loadedImage.crossOrigin = &apos;Anonymous&apos;;
    loadedImage.src = path;
    loadedImage.onload = onload;
    loadedImage.onerror = onerror;

    // var thisRef = this;
    loadedImage.onload = function() {
        // create texture
        var gl = currWebGL;
        var texture = gl.createTexture();
        if (!texture){ console.error(&quot;Failed to generate gl texture name.&quot;); return -1; }

        if(!model.isPremultipliedAlpha()){
            // &#x4E57;&#x7B97;&#x6E08;&#x30A2;&#x30EB;&#x30D5;&#x30A1;&#x30C6;&#x30AF;&#x30B9;&#x30C1;&#x30E3;&#x4EE5;&#x5916;&#x306E;&#x5834;&#x5408;
            // emmmm, maybe do something for textures with alpha layer.
            gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
        }
        gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
        gl.activeTexture(gl.TEXTURE0);
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,
                      gl.UNSIGNED_BYTE, loadedImage);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
        gl.generateMipmap(gl.TEXTURE_2D);



        model.setTexture(no, texture);

        // &#x30C6;&#x30AF;&#x30B9;&#x30C1;&#x30E3;&#x30AA;&#x30D6;&#x30B8;&#x30A7;&#x30AF;&#x30C8;&#x3092;&#x89E3;&#x653E;
        // Release the texture object to prevent buffer overruns.
        texture = null;

        if (typeof callback == &quot;function&quot;) callback();
    };

    loadedImage.onerror = function() {
        console.error(&quot;Failed to load image : &quot; + path);
    }
}


//============================================================
//    PlatformManager # parseFromBytes(buf)

//============================================================

/**
* @name jsonParseFromBytes
* @desc parse json file into arrays
* @param {raw} buf
* @returns {Array}jsonObj
* @memberOf PlatformManager
*/
PlatformManager.prototype.jsonParseFromBytes = function(buf){

    var jsonStr;
    var bomCode = new Uint8Array(buf, 0, 3);
    if (bomCode[0] == 239 &amp;&amp; bomCode[1] == 187 &amp;&amp; bomCode[2] == 191) {
        jsonStr = String.fromCharCode.apply(null, new Uint8Array(buf, 3));
    } else {
        jsonStr = String.fromCharCode.apply(null, new Uint8Array(buf));
    }

    var jsonObj = JSON.parse(jsonStr);

    return jsonObj;
};



//============================================================
//    PlatformManager # log()
//============================================================

/**
* @name log
* @desc output log in console
* @param {string} txt
* @returns null
* @memberOf PlatformManager
*/
PlatformManager.prototype.log             = function(txt/*String*/)
{
    console.log(txt);
}
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
